package cifg;

import com.iid.flappybird.R;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cifgb.CSSDBEAN;
import cifgu.SKINADATPEW;
import cifgu.SKINADATPEV;

import java.util.ArrayList;
import java.util.List;

public class BLSCDIDALOG {

    private ImageView img_closeDialog;
    private boolean ClickBlankCancellation = true;
    private Dialog dialog;
    private Context mContext;
    private Display display;
    private RecyclerView recyclerview;
    private SKINADATPET skinAdapter;
    private List<CSSDBEAN> skinBeanList;

    public BLSCDIDALOG(Context context) {
        this.mContext = context;
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public BLSCDIDALOG init() {
        // 获取Dialog布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.bottom_list_single_choice_dialog, null);
        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());
        // 获取自定义Dialog布局中的控件
        img_closeDialog = (ImageView) view.findViewById(R.id.img_closeDialog);
        recyclerview =  view.findViewById(R.id.recyclerview);

        skinBeanList = new ArrayList<>();
        skinBeanList.add(new CSSDBEAN(R.drawable.skin_cattle,"Dongfu Bull",1));
        skinBeanList.add(new CSSDBEAN(R.drawable.skin_hema,"Happy Hippo",2));
        skinBeanList.add(new CSSDBEAN(R.drawable.skin_monkey,"Shining Monkey",3));
        skinBeanList.add(new CSSDBEAN(R.drawable.skin_pony,"Whirlwind Horse",4));
        skinBeanList.add(new CSSDBEAN(R.drawable.skin_rat,"Purple haired mouse",5));
        skinBeanList.add(new CSSDBEAN(R.drawable.skin_tiger,"Xiaotian Tiger",6));

        // 定义Dialog布局和参数
        dialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    /**
     * 设置条目布局
     */
    private void setSheetItems() {
        skinAdapter = new SKINADATPET();
        skinAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (position >= skinAdapter.getData().size()) {
                return;
            }
            try {
                SKINADATPEV.setInt(mContext,SKINADATPEW.Skin_Id,skinBeanList.get(position).id);
                if (dialog != null){
                    dialog.dismiss();
                }

            } catch (IndexOutOfBoundsException arrayOuOfBoundsEx) {
                arrayOuOfBoundsEx.printStackTrace();
            }
        });

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setAdapter(skinAdapter);
        skinAdapter.setNewInstance(skinBeanList);
    }


    public void show() {
        img_closeDialog.setOnClickListener((view) -> {
            if (dialog != null){
                dialog.dismiss();
            }
        });
        setSheetItems();
        if (mContext instanceof Activity) {
            Activity activity = (Activity) mContext;
            if (!activity.isFinishing()) {
                dialog.setCanceledOnTouchOutside(ClickBlankCancellation);
                dialog.show();
            }
        }
    }


}
